﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace common.controls
{
    public partial class baseReportViewer : CrystalDecisions.Windows.Forms.CrystalReportViewer
    {
        public baseReportViewer()
        {
            InitializeComponent();
            this.ShowParameterPanelButton = false;
            this.ShowRefreshButton = false;
        }
        public bool myShowPrintDialog = true;
        public event common.OnPrinted myOnPrinted = null;

        delegate DialogResult ShowThePrintDialog();
        public void PrintReport(ReportClass myReport, myTypes.PrintLayout printLayout, myTypes.PrintLayout defaultPrintLayout)
        {
            //To solve focus problem. See http://www.canbal.com/index.php?/Using-C-Sharp/resolved-printdialog-focus-problem.html
            myPrintDialog.UseEXDialog = false;

            System.Drawing.Printing.PrinterSettings printerSettings = null;
            if (this.myShowPrintDialog)
            {
                if (myPrintDialog.ShowDialog() != DialogResult.OK) return;
                printerSettings = myPrintDialog.PrinterSettings;
            }
            else printerSettings = new System.Drawing.Printing.PrinterSettings();

            myReport.PrintOptions.PrinterName = printerSettings.PrinterName;
            reportLibs.SetLayout(myReport, printLayout, defaultPrintLayout);
            myReport.PrintToPrinter(printerSettings.Copies, printerSettings.Collate, printerSettings.FromPage, printerSettings.ToPage);
            if (this.myOnPrinted != null) myOnPrinted(this);
        }

        public override void PrintReport()
        {
            System.Drawing.Printing.PrinterSettings printerSettings = null;
            if (this.myShowPrintDialog)
            {
                if (myPrintDialog.ShowDialog() != DialogResult.OK) return;
                printerSettings = myPrintDialog.PrinterSettings;
            }
            else printerSettings = new System.Drawing.Printing.PrinterSettings();
            ReportClass myReport = (ReportClass)this.ReportSource;
            myReport.PrintOptions.PrinterName = printerSettings.PrinterName;
            myReport.PrintToPrinter(printerSettings.Copies, printerSettings.Collate, printerSettings.FromPage, printerSettings.ToPage);
            if (this.myOnPrinted != null) myOnPrinted(this);
        }
    }
}
